import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/mine/my_purchase_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/mine/widgets/purchase_item.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyPurchasePage extends StatefulWidget {
  const MyPurchasePage({super.key});

  @override
  State<MyPurchasePage> createState() => _MyPurchasePageState();
}

class _MyPurchasePageState extends State<MyPurchasePage> {
  final MyPurchaseLogic logic = Get.put(MyPurchaseLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyPurchaseLogic>(builder: (logic) {
      return Scaffold(
        appBar: Styles.appBar(title: const QsText('我的订单')),
        backgroundColor: Colours.scaffoldBg,
        body: Column(
          children: [
            Container(
              color: Colours.white,
              height: 40,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: Styles.tabbar(
                        controller: logic.tabController,
                        indicatorBottom: -6,
                        labelPadding: const EdgeInsets.symmetric(horizontal: 6),
                        scrollable: true,
                        tabs: logic.tabs.map((e) => QsText(e)).toList()),
                  ),
                ],
              ),
            ),
            Expanded(
              child: EasyRefresh(
                  onRefresh: logic.getRefresh,
                  onLoad: logic.loadMore,
                  footer:
                      Styles.refreshFooter(position: IndicatorPosition.locator),
                  controller: logic.refreshController,
                  child: TabBarView(
                      controller: logic.tabController,
                      children: List.generate(
                          logic.tabs.length,
                          (index) => LoadingWidget<bool>(
                                isLoading: logic.pageList[index] == null,
                                noData: const NoDataWidget(
                                  tip: '你还没有订单哦',
                                ),
                                data: logic.pageList[index]?.length == 0,
                                child: () => ListView(
                                    children: List<Widget>.generate(
                                            logic.pageList[index]?.length ?? 0,
                                            (i) => PurchaseItem(
                                                data: logic.pageList[index]![i],
                                                type:
                                                    PurchaseType.values[index]))
                                        .append(const FooterLocator())),
                              )))),
            ),
          ],
        ),
      );
    });
  }
}
